﻿Imports WiremetModel.Specification.GeneralSpecifications
Imports WiremetModel.Entities.OEM

Public Class frmKlientSzukanie

    Private mvarDbHolder As clsDataBase
    Private mvarWyznFrm As Boolean
    Private mvarWyznUruchomienia As Boolean
    Private mvarIdWybranegoKlienta As Integer = 0
    Private mvarId As Integer = -1
    Private mvarUzytkownik As clsUzytkownik


    Public Sub New()

        InitializeComponent()

        mvarDbHolder = New clsDataBase(VAR_CONNECTION_PARAMETERS, VAR_ERROR_HANDLER)
        mvarUzytkownik = CType(clsUzytkownik.Instance(), clsUzytkownik)
        Uprawnienia()

    End Sub


    Public Sub New(ByVal WzynFrm As Boolean, ByVal Id As Integer)

        ' To wywołanie jest wymagane przez projektanta.
        InitializeComponent()

        ' Dodaj inicjację po wywołaniu funkcji InitializeComponent().

        mvarDbHolder = New clsDataBase(VAR_CONNECTION_PARAMETERS, VAR_ERROR_HANDLER)
        mvarUzytkownik = CType(clsUzytkownik.Instance(), clsUzytkownik)

        mvarWyznFrm = WzynFrm
        mvarId = Id
        mvarIdWybranegoKlienta = 0
        Uprawnienia()

    End Sub

    Private Sub Uprawnienia()

        If mvarUzytkownik.Uprawnienia("biuro") = 0 OrElse mvarUzytkownik.Uprawnienia("biuro") = 1 Then
            cmdDodaj.Enabled = False
            cmdEdytuj.Enabled = False
        End If

    End Sub

    Private Sub ZnajdzKlienta()

        If dgvInformacje.Rows.Count > 0 Then
            For lvarI As Integer = 0 To dgvInformacje.Rows.Count - 1
                If CInt(dgvInformacje.Rows(lvarI).Cells("colIdKlienta").Value) = mvarId Then

                    dgvInformacje.ClearSelection()
                    dgvInformacje.Rows.Item(lvarI).Selected = True

                    If lvarI > 10 Then
                        dgvInformacje.FirstDisplayedScrollingRowIndex = lvarI - 10
                    Else
                        dgvInformacje.FirstDisplayedScrollingRowIndex = 0
                    End If
                    Exit For

                End If
            Next
        End If

    End Sub

    Public Sub Szukaj()

        Dim lvarCommand As String
        Dim lvarI As Integer
        Dim lvarWyzn As Boolean = False

        If txtNazwaSkrocona.Text = String.Empty And txtNazwaPelna.Text = String.Empty And txtMiejscowosc.Text = String.Empty And txtNip.Text = String.Empty Then
            lvarCommand = String.Empty
        Else
            lvarCommand = lvarCommand + " WHERE"
            If txtNazwaSkrocona.TextLength > 0 Then
                lvarCommand = lvarCommand + " UPPER (klient.nazwa_skrocona) LIKE UPPER('" & txtNazwaSkrocona.Text & "%')"
                lvarWyzn = True
            End If
            If txtNazwaPelna.TextLength > 0 Then
                If lvarWyzn = True Then
                    lvarCommand = lvarCommand + " AND"
                End If
                lvarCommand = lvarCommand + " UPPER (klient.nazwa_pelna) LIKE UPPER('" & txtNazwaPelna.Text & "%')"
                lvarWyzn = True
            End If
            If txtMiejscowosc.TextLength > 0 Then
                If lvarWyzn = True Then
                    lvarCommand = lvarCommand + " AND"
                End If
                lvarCommand = lvarCommand + " UPPER (klient.miejscowosc) LIKE UPPER('" & txtMiejscowosc.Text & "%')"
                lvarWyzn = True
            End If
            If txtNip.TextLength > 0 Then
                If lvarWyzn = True Then
                    lvarCommand = lvarCommand + " AND"
                End If
                lvarCommand = lvarCommand + " UPPER (klient.nip) LIKE UPPER('" & txtNip.Text & "%')"
                lvarWyzn = True
            End If
        End If

        Dim lvarTmp As clsInhKlient
        lvarTmp = New clsInhKlient
        lvarTmp.SzukajKlientow(mvarDbHolder, VAR_ERROR_HANDLER, lvarCommand)

        dgvInformacje.Rows.Clear()
        If lvarTmp.Klient.Count = 0 Then
            MessageBox.Show("Nie ma takich klientów.", "WIREMET")
        Else
            For Each lvarKlient As clsKlient In lvarTmp.Klient
                lvarI += 1
                dgvInformacje.Rows.Add(lvarI, lvarKlient.Id, lvarKlient.NazwaSkrocona, lvarKlient.NazwaPelna, lvarKlient.Miejscowosc, lvarKlient.Nip)
                dgvInformacje.Rows(lvarI - 1).Tag = lvarKlient
            Next
        End If

        mvarWyznUruchomienia = False
        'ustawienie widoku dgvInformacje tak aby zaznaczona liniia była widoczna
        If Not mvarWyznFrm Then
            If dgvInformacje.Rows.Count > 0 Then
                For lvarI = 0 To dgvInformacje.Rows.Count - 1
                    If CInt(dgvInformacje.Rows(lvarI).Cells("colIdKlienta").Value) = mvarIdWybranegoKlienta Then

                        dgvInformacje.ClearSelection()
                        dgvInformacje.Rows.Item(lvarI).Selected = True

                        If lvarI > 10 Then
                            dgvInformacje.FirstDisplayedScrollingRowIndex = lvarI - 10
                        Else
                            dgvInformacje.FirstDisplayedScrollingRowIndex = 0
                        End If
                        Exit For
                    End If
                Next
            End If
        End If

        If mvarWyznFrm Then
            ZnajdzKlienta()
        End If

    End Sub

    Private Sub dgvInformacje_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvInformacje.CellMouseDoubleClick

        If dgvInformacje.Rows.Count = 0 Then
            MessageBox.Show("Wybierz klienta", "WIREMET")
        Else
            mvarIdWybranegoKlienta = CInt(dgvInformacje.SelectedRows(0).Cells("colIdKlienta").Value)
            If mvarWyznFrm Then
                Me.Close()
            Else
                Dim lvarId As Integer
                lvarId = CInt(dgvInformacje.SelectedRows.Item(0).Cells("colIdKlienta").Value)
                Dim lvarFrm As New frmKlient(lvarId, False)

                With lvarFrm
                    .MdiParent = frmKlient.ActiveForm
                    .Show()
                End With
            End If
        End If

    End Sub

    Private Sub cmdSzukaj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSzukaj.Click
        Szukaj()
    End Sub

    Private Sub cmdWyczysc_Click(sender As System.Object, e As System.EventArgs) Handles cmdWyczysc.Click

        txtNazwaPelna.Text = String.Empty
        txtNazwaSkrocona.Text = String.Empty
        txtMiejscowosc.Text = String.Empty
        txtNip.Text = String.Empty
        txtNazwaSkrocona.Focus()

    End Sub

    Private Sub cmdDodaj_Click(sender As System.Object, e As System.EventArgs) Handles cmdDodaj.Click

        If Not mvarWyznFrm Then
            Dim lvarFrm As New frmKlient(0, False)
            With lvarFrm
                .MdiParent = mdiMain.ActiveForm
                .Show()
            End With
        Else
            Dim lvarFrm As New frmKlient(0, False)
            lvarFrm.ShowDialog()
        End If

    End Sub

    Private Sub cmdEdytuj_Click(sender As System.Object, e As System.EventArgs) Handles cmdEdytuj.Click

        If dgvInformacje.Rows.Count = 0 Then
            MessageBox.Show("Wybierz klienta", "WIREMET")
        Else
            Dim lvarId As Integer
            mvarIdWybranegoKlienta = CInt(dgvInformacje.SelectedRows(0).Cells("colIdKlienta").Value)
            lvarId = CInt(dgvInformacje.SelectedRows.Item(0).Cells("colIdKlienta").Value)
            mvarId = CInt(dgvInformacje.SelectedRows.Item(0).Cells("colIdKlienta").Value)
            If Not mvarWyznFrm Then
                Dim lvarFrm As New frmKlient(lvarId, False)
                With lvarFrm
                    .MdiParent = frmKlient.ActiveForm
                    .Show()
                End With
            Else
                Dim lvarFrm As New frmKlient(lvarId, False)
                lvarFrm.ShowDialog()
                ' Szukaj()
            End If

        End If
    End Sub

    Private Sub cmdWyczyscInformacje_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWyczyscInformacje.Click
        dgvInformacje.Rows.Clear()
    End Sub


    'Private Sub frmKlientSzukanie_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    '    mvarWyznUruchomienia = True
    '    ' cmdSzukaj.PerformClick()
    'End Sub

    Private Sub frmKlientSzukanie_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If mvarWyznFrm Then
            txtNazwaSkrocona.Text = String.Empty
        Else
            txtNazwaSkrocona.Text = "A"
        End If
        cmdSzukaj.PerformClick()

    End Sub

    Private Sub frmSzukajKlienta_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        mvarDbHolder.Close()
    End Sub

#Region "KeyPress"

    Private Sub txtNazwaSkrocona_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNazwaSkrocona.KeyPress
        If e.KeyChar = Char.ConvertFromUtf32(Keys.Enter) Then
            Szukaj()
        End If
    End Sub

    Private Sub txtNazwaPelna_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNazwaPelna.KeyPress
        If e.KeyChar = Char.ConvertFromUtf32(Keys.Enter) Then
            Szukaj()
        End If
    End Sub

    Private Sub txtMiejscowosc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMiejscowosc.KeyPress
        If e.KeyChar = Char.ConvertFromUtf32(Keys.Enter) Then
            Szukaj()
        End If
    End Sub

    Private Sub txtNip_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNip.KeyPress
        If e.KeyChar = Char.ConvertFromUtf32(Keys.Enter) Then
            Szukaj()
        End If
    End Sub

#End Region

#Region "TextChanged"

    Private Sub txtNazwaSkrocona_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNazwaSkrocona.TextChanged
        mvarIdWybranegoKlienta = 0
    End Sub

    Private Sub txtNazwaPelna_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNazwaPelna.TextChanged
        mvarIdWybranegoKlienta = 0
    End Sub

    Private Sub txtMiejscowosc_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMiejscowosc.TextChanged
        mvarIdWybranegoKlienta = 0
    End Sub

    Private Sub txtNip_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNip.TextChanged
        mvarIdWybranegoKlienta = 0
    End Sub

#End Region

    Private Sub btn_ZamowieniaKlientaReport_Click(sender As System.Object, e As System.EventArgs) Handles btn_ZamowieniaKlientaReport.Click
        Try
            If dgvInformacje.SelectedRows.Count = 0 Then
                Throw New Exception("Proszę, wybierz klienta, aby kontynuować.")
            End If

            Dim klientId As Integer = CInt(dgvInformacje.SelectedRows(0).Cells("colIdKlienta").Value)
            Dim idSpecification As New IdSpecification(Of Klient)(klientId)
            Dim unitOfWork As New UnitOfWork()
            Dim klientRepository As New Repository(Of Klient)(unitOfWork)
            Dim klient As Klient = klientRepository.GetEntityMatching(idSpecification)

            Dim dateRangeDialog As New DateRangeDialog()
            If dateRangeDialog.ShowDialog() <> Windows.Forms.DialogResult.OK Then
                Return
            End If

            Dim lowerBoundary As Date = dateRangeDialog.LowerBoundary
            Dim upperBoundary As Date = dateRangeDialog.UpperBoundary

            Cursor = Cursors.WaitCursor

            Dim reportService As New ZamowieniaKlientaReportService()
            Dim report As ZamowieniaKlientaReport = reportService.GenerateReport(klient, lowerBoundary, upperBoundary)

            If report IsNot Nothing Then
                Dim printViewer As frmPrintViewer = New frmPrintViewer(report, False)
                printViewer.WindowState = FormWindowState.Maximized
                printViewer.ShowDialog()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub
End Class
